# -*- encoding: utf-8 -*-
# @Author: SWHL
# @Contact: liekkaskono@163.com
from abc import ABC, abstractmethod
from pathlib import Path
from typing import List

from .utils.typings import OutputFormat
from .utils.utils import write_txt


class ExportStrategy(ABC):
    @abstractmethod
    def export(
        self,
        save_dir: Path,
        save_name: str,
        srt_result: List[str],
        ass_result: List[str],
        txt_result: List[str],
    ):
        pass


class TxtExportStrategy(ExportStrategy):
    def export(
        self,
        save_dir: Path,
        save_name: str,
        srt_result: List[str],
        ass_result: List[str],
        txt_result: List[str],
    ):
        file_path = save_dir / f"{save_name}.txt"
        write_txt(file_path, txt_result)


class SrtExportStrategy(ExportStrategy):
    def export(
        self,
        save_dir: Path,
        save_name: str,
        srt_result: List[str],
        ass_result: List[str],
        txt_result: List[str],
    ):
        file_path = save_dir / f"{save_name}.srt"
        write_txt(file_path, srt_result)


class AssExportStrategy(ExportStrategy):
    def export(
        self,
        save_dir: Path,
        save_name: str,
        srt_result: List[str],  # unused here but kept for signature
        ass_result: List[str],
        txt_result: List[str],
    ):
        header = [
            "[Script Info]",
            "; Script generated by RapidVideOCR",
            "ScriptType: v4.00+",
            "PlayResX: 1920",
            "PlayResY: 1080",
            "",
            "[V4+ Styles]",
            "Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, "
            "Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, "
            "Alignment, MarginL, MarginR, MarginV, Encoding",
            "Style: Default,Arial,54,&H00FFFFFF,&H0000FFFF,&H00000000,&H64000000,0,0,0,0,100,100,0,0,1,2,2,2,10,10,10,1",
            "",
            "[Events]",
            "Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text",
        ]

        file_path = save_dir / f"{save_name}.ass"
        write_txt(file_path, header + [""] + ass_result)


class AllExportStrategy(ExportStrategy):
    def export(
        self,
        save_dir: Path,
        save_name: str,
        srt_result: List[str],
        ass_result: List[str],
        txt_result: List[str],
    ):
        txt_export = TxtExportStrategy()
        srt_export = SrtExportStrategy()
        ass_export = AssExportStrategy()

        txt_export.export(save_dir, save_name, srt_result, ass_result, txt_result)
        srt_export.export(save_dir, save_name, srt_result, ass_result, txt_result)
        ass_export.export(save_dir, save_name, srt_result, ass_result, txt_result)


class ExportStrategyFactory:
    @staticmethod
    def create_strategy(out_format: str = OutputFormat.ALL.value) -> ExportStrategy:
        strategies = {
            OutputFormat.TXT.value: TxtExportStrategy(),
            OutputFormat.SRT.value: SrtExportStrategy(),
            OutputFormat.ASS.value: AssExportStrategy(),
            OutputFormat.ALL.value: AllExportStrategy(),
        }

        if strategy := strategies.get(out_format):
            return strategy
        raise ValueError(f"Unsupported output format: {out_format}")
